package com.citywithincity.db;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class Page<T> {
	@SuppressWarnings("rawtypes")
	public static final Page EMPTY_PAGE = new Page();
	static{
		EMPTY_PAGE.setList(Collections.EMPTY_LIST);
		EMPTY_PAGE.setPage(1);
		EMPTY_PAGE.setPageSize(1);
		EMPTY_PAGE.setTotalPages(1);
		EMPTY_PAGE.setTotalRows(0);
		
	}
	
	
	public void fromPage(Page<?> page){
		
		totalPages = page.getTotalPages();
		totalRows = page.getTotalRows();
		list = (List<T>) page.getList();
		pageSize = page.getPageSize();
		setExtra(page.getExtra());
		this.page = page.getPage();
		
	}
	
	public static <T> Page<T> wrapPage(Page<?> page,List<T> list){
		Page<T> p = new Page<T>();
		p.setExtra(page.getExtra());
		p.setTotalPages(page.getTotalPages());
		p.setTotalRows(page.getTotalRows());
		p.setPage(page.getPage());
		p.setPageSize(page.getPageSize());
		p.setList(list);
		
		return p;
	}
	public static <T> Page<T> wrapPosition(int position,int pageSize,int count,Collection<T> set){
		int page = position / pageSize + 1;
		int total;
		if(count%pageSize==0){
			total = count / pageSize;
		}else{
			total = count / pageSize + 1;
		}
		
		Page<T> p = new Page<T>();
		
		p.setTotalPages(total);
		p.setTotalRows(count);
		p.setPage(page);
		p.setPageSize(pageSize);
		List<T> list = new ArrayList<T>(set.size());
		list.addAll(set);
		p.setList(list);
		
		return p;
		
	}
	
	
	/**
	 * 从0开始
	 */
	private Integer page;
	private Integer pageSize;
	private Integer totalPages;
	private Integer totalRows;
	private Map<String, Object> extra;
	private List<T> list;
	
	
	public boolean isLast(){
		return getPage() >= getTotalPages();
	}
	
	public boolean isFirst(){
		return page <= 1;
	}
	
	public boolean isEmpty(){
		return list == null || list.isEmpty();
	}
	
	
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(Integer totalPages) {
		this.totalPages = totalPages;
	}
	public Integer getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(Integer totalRows) {
		this.totalRows = totalRows;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	
	
	public void toMap(Map<String, Object> map) {
		map.put("result", getList());
		map.put("page", getPage());
		map.put("total", getTotalPages());
		map.put("totalRows", getTotalRows());
		if(extra!=null){
			map.put("extra", extra);
		}
	}

	public Map<String, Object> getExtra() {
		return extra;
	}

	public void setExtra(Map<String, Object> extra) {
		this.extra = extra;
	}
}
